// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env:'mupp-5gs2863z7cf6873f'
})

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    const shop=cloud.database().collection('shop')
    console.log(event)
    let openid=wxContext.OPENID
    let res=await shop.where({
        _id:event._id
    }).get()
    console.log('res',res)
    let zan=res.data[0].zan
    let cai=res.data[0].cai
    if(event.up){
        if(event.change){
            console.log('点赞')
            zan.push(openid)
            if(cai.indexOf(openid)>-1){
                cai.splice(cai.indexOf(openid),1)
            }
        }else{
            console.log('取消点赞')
            zan.splice(zan.indexOf(openid),1)
        }
    }else{
        console.log('踩')
        if(event.change){
            console.log('点踩')
            cai.push(openid)
            if(zan.indexOf(openid)>-1){
                zan.splice(zan.indexOf(openid),1)
            }
        }else{
            console.log('取消踩')
            cai.splice(cai.indexOf(openid),1)
        }
        
    }
    shop.where({
        _id:event._id
    }).update({
        data:{
            zan,cai
        }
    })
        
    return {
        
    }
}